﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

/*
 * Image found at:
 * Scary Corridor. Digital image. HDW. HDW, n.d. Web. 
 * 29 Sept. 2014. <http://hdw.eweb4.com/out/1114207.html>.
 * */
namespace CSE360_SudokuProject
{
    public partial class MainMenu : Form
    {
        //variables
        LogInMenu loginParent;
        MainGameScreen9x9 theGame9x9;
        MainGameScreen16x16 theGame16x16;
        HighScoreMenu theScores;
        UsersGuide theInstructions;
               
        bool endGame; // checks to see if the main menu is closing to play game or if program should close
        public bool highScoreOpened; // checks if highscore is already open
        public bool instructionOpened; //checks if instructions is already open

        public MainMenu(LogInMenu loginparent)
        {
            //constructor
            InitializeComponent();
            loginParent = loginparent;
            endGame = true;
            highScoreOpened = false;
            instructionOpened = false;
        }

        private void MainMenu_Load(object sender, EventArgs e)
        {
            //initializes combo box
            DifficultycomboBox.Text = "Select Difficulty";

            //makes labe transparent
            var pos = this.PointToScreen(label1.Location);
            pos = pictureBox1.PointToClient(pos);
            label1.Parent = pictureBox1;
            label1.Location = pos;
            label1.BackColor = Color.Transparent;

            //checks if returning from winning a puzzle
            if(FileManager.youWon)
            {
                //goes to highscore menu and updatas highscores
                highScoreOpened = true;
                theScores = new HighScoreMenu(this);
                theScores.SaveNewHighScores();
                theScores.Show();
                FileManager.youWon = false;             
            }          
        }

        

        private void PlayButton_Click(object sender, EventArgs e)
        {
            //switch statement for the combobox to select difficulty
            switch (DifficultycomboBox.Text)
            {
                    //initialize the selected board and nesscary variables
                    //then creates the form and closes the main menu form
                case "Easy":
                    ThePuzzleGenerator.EasyBoard();
                    ThePuzzleGenerator.is9x9 = true;
                    ThePuzzleGenerator.is16x16 = false;
                    FileManager.difficulty = 1;
                    FileManager.currentScore = 0;
                    FileManager.currentTimeMinute = 0;
                    FileManager.currentTimeSecond = 0; 
                    theGame9x9 = new MainGameScreen9x9(loginParent);
                    theGame9x9.Show();
                    endGame = false;
                    this.Close();
                    break;
                case "Medium":
                    ThePuzzleGenerator.MediumBoard();
                    ThePuzzleGenerator.is9x9 = true;
                    ThePuzzleGenerator.is16x16 = false;
                    FileManager.difficulty = 2;
                    FileManager.currentScore = 0;
                    FileManager.currentTimeMinute = 0;
                    FileManager.currentTimeSecond = 0; 
                    theGame9x9 = new MainGameScreen9x9(loginParent);
                    theGame9x9.Show();
                    endGame = false;
                    this.Close();
                    break;
                case "Hard":
                    ThePuzzleGenerator.HardBoard();
                    ThePuzzleGenerator.is9x9 = true;
                    ThePuzzleGenerator.is16x16 = false;
                    FileManager.difficulty = 3;
                    FileManager.currentScore = 0;
                    FileManager.currentTimeMinute = 0;
                    FileManager.currentTimeSecond = 0; 
                    theGame9x9 = new MainGameScreen9x9(loginParent);
                    theGame9x9.Show();
                    endGame = false;
                    this.Close();
                    break;
                case "Hell":
                    ThePuzzleGenerator.HellBoard();
                    ThePuzzleGenerator.is9x9 = false;
                    ThePuzzleGenerator.is16x16 = true;
                    FileManager.difficulty = 4;
                    FileManager.currentScore = 0;
                    FileManager.currentTimeMinute = 0;
                    FileManager.currentTimeSecond = 0; 
                    theGame16x16 = new MainGameScreen16x16(loginParent); //16x16grid
                    theGame16x16.Show();
                    endGame = false;
                    this.Close();
                    break;
                default:
                    //if no difficulty was selected tells the user to select one
                    MessageBox.Show("Please select a difficulty");
                    break;
            }
        }

        private void HighScoreButton_Click(object sender, EventArgs e)
        {
            //first checks to make sure there is not already a highscore open
            if (highScoreOpened == false)
            {
                //then creates one in the background (main menu stays open)
                highScoreOpened = true;
                theScores = new HighScoreMenu(this);
                theScores.Show();
            }
        }

        private void InstructionsButton_Click(object sender, EventArgs e)
        {
            //first checks to make sure there is not already a highscore open
            if (instructionOpened == false)
            {
                //then creates one in the background (main menu stays open)
                instructionOpened = true;
                theInstructions = new UsersGuide(this);
                theInstructions.Show();
            }
        }

        private void ResumeButton_Click(object sender, EventArgs e)
        {
            //first checks if there is a previous puzzle to resume and loads the data
            if(FileManager.GetBoardData())
            {
                //then checks if it is a 9x9 or 16x16 and loads the correct form
                if (ThePuzzleGenerator.is9x9)
                {
                    theGame9x9 = new MainGameScreen9x9(loginParent);
                    theGame9x9.Show();
                }
                else if (ThePuzzleGenerator.is16x16)
                {
                    theGame16x16 = new MainGameScreen16x16(loginParent);
                    theGame16x16.Show();
                }
                endGame = false;
                this.Close();
            }
            else
                //message if there is no puzzle
                MessageBox.Show("No previous gameboard");
        }

        private void QuitButton_Click(object sender, EventArgs e)
        {
            //closes the main menu (will also close login parent to end game)
            this.Close();
        }

        private void LogOutButton_Click(object sender, EventArgs e)
        {
            //re-enables and shows the login parent and closes the main menu form
            loginParent.Enabled = true;
            loginParent.Visible = true;
            endGame = false;
            this.Close();
        }

        private void MainMenu_FormClosed(object sender, FormClosedEventArgs e)
        {
            //checks if it needs to close login parent to end the game
            if (endGame == true)
                loginParent.Close();

            //checks if it needs to close a highscore form
            if(theScores != null)
                theScores.Close();

            //checks if it needs to close a instruction form
            if (theInstructions != null)
                theInstructions.Close();
        }
    }
}
